package net.asgarli.ivault.api;

import java.util.HashMap;
import java.util.Map;

/**
 * Static factory class that instantiates key supply methods.
 * Every key supply method should register itself with this class. 
 * @author Elchin Asgarli
 * @version 0.1
 * @since 0.1
 */
public class KeySupplyFactory {
	/** List of all available Key Supply Methods */
	static private Map<Integer, Class<? extends KeySupplyMethod>> keySupplyMethods = 
		new HashMap<Integer, Class<? extends KeySupplyMethod>>();

	static {
		// Register known key supply types
		registerKeySupply(0, UserKeySupply.class);
		registerKeySupply(1, PasswordProtectedKeySupply.class);
	}
	
	/**
	 * Register given KeySupplyMethod with given code.
	 * @param code the code under which KeySupplyMethod must be registered
	 * @param method the KeySupplyMethod
	 */
	public static void registerKeySupply(int code, Class<? extends KeySupplyMethod> method) {
		keySupplyMethods.put(code, method);
	}
	
	/**
	 * Factory method to get KeySupplyMethod based on code.
	 * @param code
	 * @return
	 */
	public static KeySupplyMethod getInstance(int code) {
		try {
			return keySupplyMethods.get(code).newInstance();
		} catch (InstantiationException e) {
			// TODO Log
			return null;
		} catch (IllegalAccessException e) {
			// TODO Log
			return null;
		}
	}
}
